package com.yf.cmw.dao.buss;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.yf.cmw.dao.BaseDao;
import com.yf.cmw.entity.MachineCfgEntity;

public class MachineCfgDao extends BaseDao {

    public static final String TAB_NAME = "APP_MachineCfg";

    public MachineCfgDao(Context context) {
        super(context);
    }

    @Override
    protected String dropTabSql() {
//        StringBuilder sbSql = new StringBuilder();
//        sbSql.append("drop table ").append(TAB_NAME);
        return null;
    }

    @Override
    protected String getCreateTabSql() {
        StringBuilder sbSql = new StringBuilder();
        sbSql.append("create table ").append(TAB_NAME).append("(")
                .append("_id  INTEGER PRIMARY KEY AUTOINCREMENT,")
                .append("cfgVersion INTEGER,")
                .append("appVersion VARCHAR(50),")
                .append("mcode VARCHAR(100),")
                .append("adPath VARCHAR(150),")
                .append("serverUrl VARCHAR(150),")
                .append("minutes INTEGER,")
                .append("createTime VARCHAR(20),")
                .append("modifyTime VARCHAR(20),")
                .append("modifier VARCHAR(20),")
                .append("safeCode VARCHAR(20)")
                .append(")");
        return sbSql.toString();
    }

    /**
     *  保存机器配置
     * @param obj
     * @return
     */
    public Long save(MachineCfgEntity obj){
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("cfgVersion", obj.getCfgVersion());
        values.put("appVersion", obj.getAppVersion());
        values.put("mcode", obj.getMcode());
        values.put("adPath", obj.getAdPath());
        values.put("serverUrl", obj.getServerUrl());
        values.put("minutes", obj.getMinutes());
        values.put("createTime", obj.getCreateTime());
        long id = database.insert(TAB_NAME,null, values);
        database.close();
        return id;
    }

    /**
     *  修改机器配置
     * @param obj
     * @return
     */
    public int update(MachineCfgEntity obj){
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("cfgVersion", obj.getCfgVersion());
        values.put("appVersion", obj.getAppVersion());
        values.put("mcode", obj.getMcode());
        values.put("adPath", obj.getAdPath());
        values.put("serverUrl", obj.getServerUrl());
        values.put("minutes", obj.getMinutes());
        values.put("modifyTime", obj.getModifyTime());
        values.put("modifier", obj.getModifier());
        String whereClause = " _id = ? ";
        String[] whereArgs = new String[]{obj.getId()+""};
        int result = database.update(TAB_NAME,values,whereClause, whereArgs);
        database.close();
        return result;
    }

    /**
     * 获取机器配置信息
     * @return
     */
    public MachineCfgEntity get(){
        SQLiteDatabase database = dbHelper.getReadableDatabase();

        String orderBy = " _id desc ";
        Cursor cursor = database.query(TAB_NAME, null, null, null, null, null,
                 orderBy);
        boolean next = cursor.moveToNext();
        if(!next) return null;
        Integer _id = cursor.getInt(cursor.getColumnIndex("_id"));
        Integer cfgVersion = cursor.getInt(cursor.getColumnIndex("cfgVersion"));
        String appVersion = cursor.getString(cursor.getColumnIndex("appVersion"));
        String mcode = cursor.getString(cursor.getColumnIndex("mcode"));
        String adPath = cursor.getString(cursor.getColumnIndex("adPath"));
        int surl_index = cursor.getColumnIndex("serverUrl");
        String serverUrl = "";
        if(surl_index != -1) {
            serverUrl = cursor.getString(surl_index);
        }else{
            database.execSQL("ALTER TABLE "+TAB_NAME+" ADD serverUrl VARCHAR(150)");
        }
        Integer minutes = cursor.getInt(cursor.getColumnIndex("minutes"));
        String createTime = cursor.getString(cursor.getColumnIndex("createTime"));
        String modifyTime = cursor.getString(cursor.getColumnIndex("modifyTime"));
        String modifier = cursor.getString(cursor.getColumnIndex("modifier"));
        String safeCode = cursor.getString(cursor.getColumnIndex("safeCode"));
        cursor.close();
        database.close();
        MachineCfgEntity obj = new MachineCfgEntity();
        obj.setId(_id.longValue());
        obj.setCfgVersion(cfgVersion);
        obj.setAppVersion(appVersion);
        obj.setMcode(mcode);
        obj.setAdPath(adPath);
        obj.setServerUrl(serverUrl);
        obj.setMinutes(minutes);
        obj.setCreateTime(createTime);
        obj.setModifyTime(modifyTime);
        obj.setModifier(modifier);
        obj.setSafeCode(safeCode);
        return obj;
    }
}
